// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { privacyChannel } from "../Script/privacyChannel";
import DataManager from "../manager/DataManager";
import bh_toast from "../Script/bh_toast";
import MainLayer from "./MainLayer";
import { privacyChannelVivo } from "../Script/privacyChannelVivo";

const { ccclass, property } = cc._decorator;

@ccclass
export default class WinLayer extends BaseLayer {

    panel: cc.Node = null
    btnNext: cc.Node = null
    btnShare: cc.Node = null
    videoTip: cc.Node = null;
    shartVideoicon: cc.Node = null;

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnNext = cc.find('buttons/btn_next', this.panel)
        this.btnShare = cc.find('buttons/btn_share', this.panel)
        this.videoTip = cc.find("style/panel/videotip", this.node)
        this.btnNext.on('click', this.onNextClick, this)
        this.btnShare.on('click', this.onShareClick, this);
        this.shartVideoicon = cc.find('buttons/btn_share/icon', this.panel);
        cc.find("style/panel/videotip/box", this.node).on("click", this.onClickBox, this);

        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p3")) {
                    this.shartVideoicon.active = false;
                } else {
                    this.shartVideoicon.active = true;
                }
                break
        }

        this.videoTip.on("click", this.onClickVideoLabel, this)


    }
    show(): void {
        super.show()
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.WIN)
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                let random = Math.random()
                let notshowKuangDian = random > privacyChannel.getChannel().getCtrl('p6') / 100
                if (privacyChannel.getChannel().getCtrl("p2") && notshowKuangDian) {
                    console.error("winLayerShow")
                    privacyChannel.getChannel().showInter(false)
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                this.btnShare.getComponentInChildren(cc.Label).string = "双倍奖励"
                if (notshowKuangDian) {

                } else {
                    cc.resources.load("kuangDian/kuangDian", cc.Prefab, (error, assert: cc.Prefab) => {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        let node = cc.instantiate(assert);
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(node);
                    });
                }
                if (DataManager.instance.stageModel) {
                    //挑战模式没有关卡
                    cc.sys.localStorage.setItem("challegeTime", "0:00")

                }

                break
            case "ks":
                this.btnShare.getComponentInChildren(cc.Label).string = "获得撤销道具"
                this.btnShare.getChildByName("icon").active = true
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }

                if (privacyChannel.getChannel().getCtrl("p5")) {
                    this.videoTip.active = true
                    this.btnNext.getChildByName("icon").active = true
                    this.videoTip.getChildByName("mark").active = true

                } else if (privacyChannel.getChannel().getCtrl("p5")) {
                    //0
                    this.videoTip.active = false
                    this.btnNext.getChildByName("icon").active = false
                    this.videoTip.getChildByName("mark").active = false


                }

                if ((DataManager.instance.level - 1) % 2 == 0) {
                    if (privacyChannel.getChannel().getCtrl("p6")) {
                        this.videoTip.active = false
                        this.btnNext.getChildByName("icon").active = true
                    }
                }
                if (privacyChannel.getChannel().getCtrl("p7")) {
                    cc.resources.load("prefab/ui/rewardBoxFrame1", cc.Prefab, (error, assert: cc.Prefab) => {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        let rewardBoxFrame = cc.instantiate(assert);
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(rewardBoxFrame);
                    })
                }
                break


        }
        switch (privacyChannel.channel) {
            case "vivo":
            case "vivo1":
                (privacyChannel.getChannel() as privacyChannelVivo).hasShortcutInstalled((hasShortcut) => {
                    if (!hasShortcut) {
                        (privacyChannel.getChannel() as privacyChannelVivo).installShortcut()
                    }
                });
                break
        }
    }
    onDestroy() {
        this.btnNext.off('click', this.onNextClick, this)
        this.btnShare.off('click', this.onShareClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.showInterstitialAd()
    }

    onDisable() { }

    onClickVideoLabel() {
        console.error("点击按钮")
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        this.videoTip.getChildByName("mark").active = !this.videoTip.getChildByName("mark").active
        if (this.videoTip.getChildByName("mark").active) {
            this.btnNext.getChildByName("icon").active = true
        } else {
            this.btnNext.getChildByName("icon").active = false
        }
        switch (privacyChannel.channel) {

            case "ks":
                if (privacyChannel.getChannel().getCtrl("p10")) {
                    privacyChannel.getChannel().showVideo(() => {
                        DataManager.instance.tool += 1
                        DataManager.instance.save()
                        cc.resources.load("privacy/bh_toast", cc.Prefab, (error, prefab: cc.Prefab) => {
                            if (error) {
                                console.error(error);
                                return;
                            }
                            let node = cc.instantiate(prefab);
                            this.node.parent.getChildByName("MainLayer").getComponent(MainLayer).freshTool()
                            node.getComponent(bh_toast).show("获得：撤销道具x1",
                                cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))

                        })
                    })
                }
                break
        }
    }
    onClickBox() {
        console.error("点击按钮")
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        this.videoTip.getChildByName("mark").active = !this.videoTip.getChildByName("mark").active
        if (this.videoTip.getChildByName("mark").active) {
            this.btnNext.getChildByName("icon").active = true
        } else {
            this.btnNext.getChildByName("icon").active = false
        }
        switch (privacyChannel.channel) {

            case "ks":
                if (privacyChannel.getChannel().getCtrl("p11")) {
                    privacyChannel.getChannel().showVideo(() => {
                        DataManager.instance.tool += 1
                        DataManager.instance.save()
                        cc.resources.load("privacy/bh_toast", cc.Prefab, (error, prefab: cc.Prefab) => {
                            if (error) {
                                console.error(error);
                                return;
                            }
                            let node = cc.instantiate(prefab);
                            this.node.parent.getChildByName("MainLayer").getComponent(MainLayer).freshTool()
                            node.getComponent(bh_toast).show("获得：撤销道具x1",
                                cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))

                        })
                    })
                }
                break
        }
    }
    async onNextClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        switch (privacyChannel.channel) {
            case "vivo":
            case "vivo1":
            case "oppo":
            case "xm":
                if ((DataManager.instance.level - 1) % 5 == 0) { //开箱子
                    cc.resources.load("prefab/ui/rewardBoxFrame", cc.Prefab, (error, assert: cc.Prefab) => {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        let rewardBoxFrame = cc.instantiate(assert);
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(rewardBoxFrame);
                        rewardBoxFrame.active = false
                        cc.resources.load("prefab/ui/RewardPage", cc.Prefab, (error, assert: cc.Prefab) => {
                            if (error) {
                                console.error(error);
                                return;
                            }
                            let RewardPage = cc.instantiate(assert);
                            cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(RewardPage);
                            rewardBoxFrame.active = true

                        });
                    });
                } else {
                    cc.resources.load("prefab/ui/RewardPage", cc.Prefab, (error, assert: cc.Prefab) => {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        let node = cc.instantiate(assert);
                        cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(node);

                    });
                }
                await StaticInstance.fadeManager.fadeIn()
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                StaticInstance.gameManager.onGameStart()
                break
            case "oppo1":
                if (privacyChannel.getChannel().getCtrl("p14")) {
                    if ((DataManager.instance.level - 1) % 5 == 0) { //开箱子
                        cc.resources.load("prefab/ui/rewardBoxFrame", cc.Prefab, (error, assert: cc.Prefab) => {
                            if (error) {
                                console.error(error);
                                return;
                            }
                            let rewardBoxFrame = cc.instantiate(assert);
                            cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(rewardBoxFrame);
                            rewardBoxFrame.active = false
                            cc.resources.load("prefab/ui/RewardPage", cc.Prefab, (error, assert: cc.Prefab) => {
                                if (error) {
                                    console.error(error);
                                    return;
                                }
                                let RewardPage = cc.instantiate(assert);
                                cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(RewardPage);
                                rewardBoxFrame.active = true

                            });
                        });


                    } else {
                        cc.resources.load("prefab/ui/RewardPage", cc.Prefab, (error, assert: cc.Prefab) => {
                            if (error) {
                                console.error(error);
                                return;
                            }
                            let node = cc.instantiate(assert);
                            cc.director.getScene().getChildByName('Canvas').getChildByName("UI").addChild(node);

                        });
                    }
                }
                await StaticInstance.fadeManager.fadeIn()
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                StaticInstance.gameManager.onGameStart()
                break
            case "ks":
                if (this.btnNext.getChildByName("icon").active) {
                    privacyChannel.getChannel().showVideo(async () => {

                        await StaticInstance.fadeManager.fadeIn()
                        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                        StaticInstance.gameManager.onGameStart()
                    })
                } else {
                    await StaticInstance.fadeManager.fadeIn()
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                    StaticInstance.gameManager.onGameStart()
                }
            case "mg":
                await StaticInstance.fadeManager.fadeIn()
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                StaticInstance.gameManager.onGameStart()
                break
        }


    }

    onShareClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "xm":
                privacyChannel.getChannel().showVideo(() => {
                    StaticInstance.fadeManager.fadeIn();
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false);
                    StaticInstance.gameManager.onGameStart();
                })

                break;
            case "vivo":
            case "vivo1":
                privacyChannel.getChannel().showVideo(() => {
                    StaticInstance.fadeManager.fadeIn()
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.WIN, false)
                    StaticInstance.gameManager.onGameStart()
                })
                break
            case "ks":
                privacyChannel.getChannel().showVideo(() => {
                    cc.resources.load("privacy/bh_toast", cc.Prefab, (error, prefab: cc.Prefab) => {
                        if (error) {
                            console.error(error);
                            return;
                        }
                        let node = cc.instantiate(prefab);
                        DataManager.instance.tool += 1
                        DataManager.instance.save()
                        this.node.parent.getChildByName("MainLayer").getComponent(MainLayer).freshTool()
                        node.getComponent(bh_toast).show("获得：撤销道具x1",
                            cc.director.getScene().getChildByName('Canvas').getChildByName("UI"))

                    })
                })
                break
            default:
                if (SdkManager.instance.getPlatform()) {
                    SdkManager.instance.activeShare()
                } else {
                    ToastManager.instance.show('仅支持小游戏平台', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
                }
                break
        }

    }
}
